/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.json.client;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A wrapper of {@link Map}<{@code String},{@code Object}>, just to get and set value just by
 * key(property name). With the expected types of {@link String}, {@link Number}, {@link Boolean},
 * {@link Date}, {@link Map}<{@code String}, {@code Object}>, {@link SimpleObject} and
 * {@link Collection}, this simple object can be serialized to json easily.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class SimpleObject {

  private Map<String, Object> map;

  /**
   * Creates a simple object.
   */
  public SimpleObject() {
    this(new HashMap<String, Object>());
  }

  /**
   * Creates a simple object with the given map.
   * 
   * @param map the map
   */
  public SimpleObject(Map<String, Object> map) {
    assert map != null : "The map can not be null.";
    this.map = map;
  }

  /**
   * Copies a simple object.
   * 
   * @return the copy of this
   */
  public SimpleObject copy() {
    return new SimpleObject(new HashMap<String, Object>(map));
  }

  /**
   * Gets the value of key.
   * 
   * @param key the key
   * @return the value of key.
   */
  public Object get(String key) {
    return map.get(key);
  }

  /**
   * Gets the boolean value.
   * 
   * @param key the key
   * @return the boolean value
   */
  public Boolean getBoolean(String key) {
    return (Boolean) get(key);
  }

  /**
   * Gets the Date value, if the value in this object is store as number, convert it.
   * 
   * @param key the key
   * @return the date value
   */
  public Date getDate(String key) {
    Object value = get(key);
    if (value instanceof Number) {
      return new Date(((Number) value).longValue());
    } else {
      return (Date) value;
    }
  }

  /**
   * Gets the number value and cast to int.
   * 
   * @param key the key
   * @return the int value
   */
  public int getInt(String key) {
    return getNumber(key).intValue();
  }

  /**
   * Gets the collection value.
   * 
   * @param key the key
   * @return the collection value
   */
  @SuppressWarnings("unchecked")
  public <T> List<T> getList(String key) {
    return (List<T>) get(key);
  }

  /**
   * Gets the number value.
   * 
   * @param key the key
   * @return the number value
   */
  public Number getNumber(String key) {
    return (Number) get(key);
  }

  /**
   * Gets the simple object.
   * 
   * @param key the key
   * @return the simple object
   */
  public SimpleObject getSimpleObject(String key) {
    return (SimpleObject) get(key);
  }

  /**
   * Gets the string value.
   * 
   * @param key the key
   * @return the string value
   */
  public String getString(String key) {
    return (String) get(key);
  }

  /**
   * Returns true if this map contains no key-value mappings
   * 
   * @return true if this map contains no key-value mappings
   */
  public boolean isEmpty() {
    return map.isEmpty();
  }

  /**
   * Returns the key set.
   * 
   * @return the key set
   */
  public Set<String> keySet() {
    return map.keySet();
  }

  /**
   * Sets a value for key.
   * 
   * @param key the key
   * @param value the value
   */
  public Object put(String key, Object value) {
    return map.put(key, value);
  }

  @Override
  public String toString() {
    return map.toString();
  }

  /**
   * Returns all values.
   * 
   * @return all values
   */
  public Collection<Object> values() {
    return map.values();
  }

}
